package com.ruoyi.project.module.shao;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.project.domain.YqgInformationProject;
import com.ruoyi.project.domain.YqgItemInventory;
import com.ruoyi.project.service.IYqgInformationProjectService;
import com.ruoyi.project.service.IYqgItemInventoryService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.ruoyi.common.security.utils.SecurityUtils.getLoginUser;

/**
 * 商品库存Controller
 * 
 * @author dr
 * @date 2023-05-28
 */
@RestController
@RequestMapping("/shao/inventory")
public class ShaoYqgItemInventoryController extends BaseController
{
    @Autowired
    private IYqgItemInventoryService yqgItemInventoryService;
    @Autowired
    private IYqgInformationProjectService yqgInformationProjectService;

    /**
     * 查询商品库存列表
     */
//    @RequiresPermissions("project:inventory:list")
    @GetMapping("/list")
    public TableDataInfo list(YqgItemInventory yqgItemInventory)
    {
        startPage();
        List<YqgItemInventory> list = yqgItemInventoryService.selectYqgItemInventoryList(yqgItemInventory);
        return getDataTable(list);
    }

    /**
     * 导出商品库存列表
     */
//    @RequiresPermissions("project:inventory:export")
    @Log(title = "商品库存", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YqgItemInventory yqgItemInventory)
    {
        List<YqgItemInventory> list = yqgItemInventoryService.selectYqgItemInventoryList(yqgItemInventory);
        ExcelUtil<YqgItemInventory> util = new ExcelUtil<YqgItemInventory>(YqgItemInventory.class);
        util.exportExcel(response, list, "商品库存数据");
    }

    /**
     * 获取商品库存详细信息
     */
//    @RequiresPermissions("project:inventory:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(yqgItemInventoryService.selectYqgItemInventoryById(id));
    }


    /**
     * 入库操作
     */
//    @RequiresPermissions("project:inventory:add")
    @Log(title = "商品库存", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YqgItemInventory yqgItemInventory)
    {
        int c =0;
        YqgInformationProject yqgInformationProject=new YqgInformationProject();
        yqgInformationProject.setProjectCode(yqgItemInventory.getProjectCode());
        //通过项目代码在项目信息表中查询该条项目
        List<YqgInformationProject> shoplist = yqgInformationProjectService.selectYqgInformationProjectList(yqgInformationProject);
        //如果检测到CargoAllowance字段为空则入库多少就是多少
        if(shoplist.get(0).getCargoAllowance()==null){
            shoplist.get(0).setCargoAllowance(yqgItemInventory.getStorageQuantity());
            yqgInformationProjectService.updateYqgInformationProject(shoplist.get(0));
        }else {
            //否则库中有商品那么就是相加当然0也不例外
            int a = Integer.parseInt(shoplist.get(0).getCargoAllowance());
            int b = Integer.parseInt(yqgItemInventory.getStorageQuantity());
            c =a+b;
            shoplist.get(0).setCargoAllowance(String.valueOf(c));
            yqgInformationProjectService.updateYqgInformationProject(shoplist.get(0));
        }
        //登录获得员工的nickname
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        //单纯的赋值操作
        yqgItemInventory.setDepositor(sysUser.getNickName());
        yqgItemInventory.setCargoAllowance(String.valueOf(c));
        yqgItemInventory.setNote3("手动入库");
        return toAjax(yqgItemInventoryService.insertYqgItemInventory(yqgItemInventory));
    }

    /**
     * 出库操作
     */
//    @RequiresPermissions("project:inventory:add")
    @Log(title = "商品库存", businessType = BusinessType.INSERT)
    @PostMapping("/chuku")
    public AjaxResult addNew(@RequestBody YqgItemInventory yqgItemInventory)
    {
        int c =0;
        YqgInformationProject yqgInformationProject=new YqgInformationProject();
        yqgInformationProject.setProjectCode(yqgItemInventory.getProjectCode());
        //通过项目代码在项目信息表中查询该条项目
        List<YqgInformationProject> shoplist = yqgInformationProjectService.selectYqgInformationProjectList(yqgInformationProject);
        if(shoplist.get(0).getCargoAllowance().equals("0"))
        {
            return  new AjaxResult(500,"库中已无该商品");
        } else {
            int a = Integer.parseInt(shoplist.get(0).getCargoAllowance());
            int b = Integer.parseInt(yqgItemInventory.getQuantityDelivered());
            if(b>a){
                return  new AjaxResult(500,"出库商品量大于库存量");
            }
            c= a-b;
            shoplist.get(0).setCargoAllowance(String.valueOf(c));
            yqgInformationProjectService.updateYqgInformationProject(shoplist.get(0));
            yqgItemInventory.setNote3("手动出库");
        }
        //登录获得员工的nickname
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        yqgItemInventory.setOutboundPerson(sysUser.getNickName());
        yqgItemInventory.setCargoAllowance(String.valueOf(c));
        return toAjax(yqgItemInventoryService.insertYqgItemInventory(yqgItemInventory));
    }

    /**
     * 修改商品库存
     */
//    @RequiresPermissions("project:inventory:edit")
    @Log(title = "商品库存", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YqgItemInventory yqgItemInventory)
    {
        return toAjax(yqgItemInventoryService.updateYqgItemInventory(yqgItemInventory));
    }

    /**
     * 删除商品库存
     */
//    @RequiresPermissions("project:inventory:remove")
    @Log(title = "商品库存", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yqgItemInventoryService.deleteYqgItemInventoryByIds(ids));
    }
}
